<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>医生排班</title>
    
    <!-- 引入 Bootstrap 和 Vue -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.2/dist/css/bootstrap.min.css">
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>

    <style>
        body {
            background-color: #f4f6f9;
            font-size: 16px;
        }
        .container {
            max-width: 1000px;
            background: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
        }
        h2 {
            font-size: 24px;
            font-weight: bold;
            text-align: center;
            margin-bottom: 20px;
        }
        .table th, .table td {
            text-align: center;
            vertical-align: middle;
        }
        .date-box {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            padding: 10px;
            border-radius: 8px;
            background: #f8f9fa;
            width: 120px;
            height: 100px;
            transition: background 0.3s ease;
        }
        .date-box:hover {
            background: #e9ecef;
        }
        .date-number {
            font-size: 18px;
            font-weight: bold;
            margin-bottom: 8px;
        }
        .checkboxes label {
            font-size: 14px;
            display: block;
            cursor: pointer;
        }
        .btn-container {
            text-align: center;
            margin-top: 20px;
        }
        .btn {
            font-size: 16px;
            padding: 10px 20px;
        }
    </style>
</head>
<body>
    <div class="container mt-5" id="app">
        <h2>医生排班</h2>

        <!-- 医生信息 -->
        <div class="form-group row">
            <label class="col-sm-4 col-form-label font-weight-bold">医生: <?= $goods['goods_name'] ?></label>
            <label class="col-sm-4 col-form-label font-weight-bold">科室: <?= $cat['name'] ?></label>
        </div>

        <!-- 排班表格 -->
        <table class="table table-bordered mt-4">
            <thead>
                <tr>
                    <th v-for="day in daysOfWeek" :key="day">{{ day }}</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td v-for="(day, index) in weeklySchedule" :key="index">
                        <div class="date-box">
                            <div class="checkboxes">
                                <label>
                                    <input type="checkbox" v-model="day.morning"> 上午
                                </label>
                                <label>
                                    <input type="checkbox" v-model="day.afternoon"> 下午
                                </label>
                                <label>
                                    <input type="checkbox" v-model="day.night"> 夜间
                                </label>
                            </div>
                        </div>
                    </td>
                </tr>
            </tbody>
        </table>

        <!-- 按钮 -->
        <div class="btn-container">
            <button class="btn btn-primary" @click="submitSchedule">提交排班</button>
            <button class="btn btn-secondary" @click="goBack">返回</button>
        </div>
    </div>

    <script>
        new Vue({
            el: '#app',
            data() {
                return {
                    goods_id: <?= $goods_id ?>, // 医生 ID
                    daysOfWeek: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
                    weeklySchedule: [
                        { week_day: 1, morning: false, afternoon: false, night: false },
                        { week_day: 2, morning: false, afternoon: false, night: false },
                        { week_day: 3, morning: false, afternoon: false, night: false },
                        { week_day: 4, morning: false, afternoon: false, night: false },
                        { week_day: 5, morning: false, afternoon: false, night: false },
                        { week_day: 6, morning: false, afternoon: false, night: false },
                        { week_day: 7, morning: false, afternoon: false, night: false }
                    ]
                };
            },
            mounted() {
                this.loadSchedule(); // 页面加载时获取排班数据
            },
            methods: {
                goBack() {
                    window.history.back();
                },
                // 获取排班数据
                loadSchedule() {
                    axios.get(`/store/goods/getWeeklySchedule?goods_id=${this.goods_id}`)
                        .then(response => {
                            if (response.data.schedule.length > 0) {
                                response.data.schedule.forEach(item => {
                                    const day = this.weeklySchedule.find(d => d.week_day === item.week_day);
                                    if (day) {
                                        day.morning = item.morning == 1;
                                        day.afternoon = item.afternoon == 1;
                                        day.night = item.night == 1;
                                    }
                                });
                            }
                        }).catch(error => {
                            console.error('获取排班数据失败:', error);
                        });
                },
                // 提交排班
                submitSchedule() {
    axios.post(`/store/goods/editWeeklySchedule?goods_id=${this.goods_id}`, {
        schedule: this.weeklySchedule
    }).then(response => {
        alert('排班提交成功');
        window.history.back(); // 提交成功后返回上一页
    }).catch(error => {
        console.error('提交失败:', error);
        alert('提交失败');
    });
}
            }
        });
    </script>
</body>
</html>